package com.tuu.configBean;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

@Configuration
public class RedisListenerConfig {
    /**
     * redis消息监听器容器
     * 可以添加多个监听不同话题的redis监听器，只需要把消息监听器和相应的消息订阅处理器绑定，该消息监听器
     * 通过反射技术调用消息订阅处理器的相关方法进行一些业务处理

     */
    @Bean
    RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,
                                            MessageListenerAdapter userRoleListenerAdapter,
                                            MessageListenerAdapter rolePerListenerAdapter
    ) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);

        //可以添加多个 messageListener   监听主题
        container.addMessageListener(userRoleListenerAdapter, new PatternTopic("user_role"));
        container.addMessageListener(rolePerListenerAdapter, new PatternTopic("role_permission"));
        return container;
    }


    /**
     * 消息监听器适配器，绑定消息处理器，利用反射技术调用消息处理器的业务方法
     *
     * @param redisReceiver
     * @return
     */
    @Bean
    MessageListenerAdapter rolePerListenerAdapter(RedisReceiver redisReceiver) {
        System.out.println("rolePer消息适配器进来了");
        return new MessageListenerAdapter(redisReceiver, "rolePerMessage");
    }
    @Bean
    MessageListenerAdapter userRoleListenerAdapter(RedisReceiver redisReceiver) {
        System.out.println("userRole消息适配器进来了");
        return new MessageListenerAdapter(redisReceiver, "userRoleMessage");
    }

}

